﻿using System;
using System.Windows.Forms;
using WF = System.Windows.Forms;
using System.Drawing;

namespace UpwardsTimeClock.Prototype
{
    public class AlternatingLabelControl : VisualControlBase<AlternatingLabel>
    {
        private static readonly DateTime _midnight = new DateTime(2000, 1, 1);
        private WF.Label _label;
        private bool _isAlternating;
        private int _intervalSeconds;

        private readonly Color _backcolor;
        private readonly Color _forecolor;
        private readonly Color _altBackcolor;
        private readonly Color _altForecolor;

        public AlternatingLabelControl(AlternatingLabel config)
            : base(config)
        {
            _label = LabelControl.CreateLabel(config);
            _backcolor = Color.FromName(config.BackColor);
            _forecolor = Color.FromName(config.ForeColor);
            _altBackcolor = Color.FromName(config.AltBackColor);
            _altForecolor = Color.FromName(config.AltForeColor);
        }

        protected override Control CreateVisualControl()
        {
            return _label;
        }

        public override void Initialize(Control.ControlCollection controls)
        {
            Config.TimerEventMode = TimerEventMode.Second;
            base.Initialize(controls);
        }

        public override void Start()
        {
            base.Start();
        }

        private void UpdateTimer()
        {
            _intervalSeconds++;
            if (_intervalSeconds >= Config.Interval)
            {
                _intervalSeconds = 0;
                _isAlternating = !_isAlternating;
                if (_isAlternating)
                {
                    _label.Text = Config.AltText;
                    _label.ForeColor = _altForecolor;
                    _label.BackColor = _altBackcolor;
                }
                else
                {
                    _label.Text = Config.Text;
                    _label.ForeColor = _forecolor;
                    _label.BackColor = _backcolor;
                }
                _label.Refresh();
            }
        }

        public override void Timer()
        {
            base.Timer();
            UpdateTimer();
        }
    }
}
